﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CustomerMgr.Data.Models;

namespace CustomerMgr.Data
{
    public class OrderDetailFactory
    {
        public static List<OrderDetail> SelectAll()
        {
            try
            {
                var db = new ordermanagerContext();
                return db.OrderDetails.ToList();
            }
            catch
            {
                return null;
            }
        }

        public static bool Insert(OrderDetail cus)
        {
            try
            {
                var db = new ordermanagerContext();
                db.OrderDetails.Add(cus);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static OrderDetail SelectOne(int cusId)
        {
            try
            {
                var db = new ordermanagerContext();
                return db.OrderDetails.FirstOrDefault(s => s.Id.Equals(cusId));
            }
            catch
            {
                return null;
            }
        }

        public static bool Update(OrderDetail cus)
        {
            try
            {
                var db = new ordermanagerContext();
                var orderdetail = (from i in db.OrderDetails
                           where i.Id == cus.Id
                           select i).First();
                orderdetail.OrderId = cus.OrderId;
                orderdetail.CreateDate = cus.CreateDate;
                orderdetail.Price = cus.Price;
                orderdetail.ProductName = cus.ProductName;
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool Delete(OrderDetail cus)
        {
            try
            {
                var db = new ordermanagerContext();
                var orderdetail = (from i in db.OrderDetails
                           where i.Id == cus.Id
                           select i).First();
                db.OrderDetails.Remove(orderdetail);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static Order InsertOrder(Order entity)
        {
            try
            {
                var db = new ordermanagerContext();
                db.Orders.Add(entity);
                db.SaveChanges();
                return entity;
            }
            catch
            {
                return null;
            }
        }
    }
}
